package com.ruoyi.check.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

import com.ruoyi.common.core.page.DataCountUtil;
import com.ruoyi.personnel.domain.Employee;
import com.ruoyi.personnel.mapper.EmployeeMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.poi.ss.usermodel.DateUtil;
//import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.check.mapper.AttendanceMapper;
import com.ruoyi.check.domain.Attendance;
import com.ruoyi.check.service.IAttendanceService;

import javax.management.remote.rmi._RMIConnection_Stub;
import javax.swing.text.html.HTMLWriter;
import javax.xml.transform.Source;

/**
 * 出勤管理Service业务层处理
 *
 * @author tza
 * @date 2023-03-03
 */
@Service
public class AttendanceServiceImpl implements IAttendanceService {
    @Autowired
    private AttendanceMapper attendanceMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public Map<String, Integer> getDataCount() {
        int a = 0;int b = 0;int c = 0;int d = 0;int e = 0;int f = 0;int g = 0;
        List<Attendance> attendanceList = selectAttendanceList(null);
        Map<String, Integer> map = new HashMap<>();
        for (Attendance attendance : attendanceList) {
            String week = DataCountUtil.getWeek(attendance.getOnWorkAm());
            if (week.equals("星期一")){
                a++;
                map.put(week,a);
            }
            if (week.equals("星期二")){
                b++;
                map.put(week,b);
            }
            if (week.equals("星期三")){
                c++;
                map.put(week,c);
            }
            if (week.equals("星期四")){
                d++;
                map.put(week,d);
            }
            if (week.equals("星期五")){
                e++;
                map.put(week,e);
            }
            if (week.equals("星期六")){
                f++;
                map.put(week,f);
            }
            if (week.equals("星期日")){
                g++;
                map.put(week,g);
            }
        }
        return map;
    }

    /**
     * 查询出勤管理
     *
     * @param atId 出勤管理主键
     * @return 出勤管理
     */
    @Override
    public Attendance selectAttendanceByAtId(Integer atId) {
        return attendanceMapper.selectAttendanceByAtId(atId);
    }

    /**
     * 查询出勤管理列表
     *
     * @param attendance 出勤管理
     * @return 出勤管理
     */
    @Override
    public List<Attendance> selectAttendanceList(Attendance attendance) {
        String flag = attendance.getIsQualified();

        List<Attendance> attendanceList = attendanceMapper.selectAttendanceList(attendance);
        //时间格式格式化
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
        for (Attendance att : attendanceList) {
            //存入员工的数据
            //Employee employee = employeeMapper.selectEmployeeByEmpId(att.getEmpId());
            //att.setEmpName(employee.getEmpName());
            try {
                //比对上午上班时间
                Date onWorkAm_mysql = att.getOnWorkAm();
                //格式化时间
                Date onWorkAm = simpleDateFormat.parse(simpleDateFormat.format(onWorkAm_mysql));
                //判断时间是否在08:00之前
                boolean isOnWorkAmBefore = onWorkAm.before(simpleDateFormat.parse("08:00:00"));
                if (isOnWorkAmBefore) {
                    att.setOnWorkAmMsg("合格");
                } else {
                    att.setOnWorkAmMsg("迟到");
                }
                //比对上午下班时间
                Date offWorkAm_mysql = att.getOffWorkAm();
                //格式化时间
                Date offWorkAm = simpleDateFormat.parse(simpleDateFormat.format(offWorkAm_mysql));
                //判断时间是否在12:00之后
                boolean isOffWorkAmAfter = offWorkAm.after(simpleDateFormat.parse("12:00:00"));
                if (isOffWorkAmAfter) {
                    att.setOffWorkAmMsg("合格");
                } else {
                    att.setOffWorkAmMsg("早退");
                }
                //比对下午上班时间
                Date onWorkPm_mysql = att.getOnWorkPm();
                //格式化时间
                Date onWorkPm = simpleDateFormat.parse(simpleDateFormat.format(onWorkPm_mysql));
                //判断时间是否在14:00之前
                boolean isonWorkPmBefore = onWorkPm.before(simpleDateFormat.parse("14:00:00"));
                if (isonWorkPmBefore) {
                    att.setOnWorkPmMsg("合格");
                } else {
                    att.setOnWorkPmMsg("迟到");
                }
                //比对上午下班时间
                Date offWorkPm_mysql = att.getOffWorkPm();
                //格式化时间
                Date offWorkPm = simpleDateFormat.parse(simpleDateFormat.format(offWorkPm_mysql));
                //判断时间是否在17:30之后
                boolean isOffWorkPmAfter = offWorkPm.after(simpleDateFormat.parse("17:30:00"));
                if (isOffWorkPmAfter) {
                    att.setOffWorkPmMsg("合格");
                } else {
                    att.setOffWorkPmMsg("早退");
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        List<Attendance> attendances = new ArrayList<>();

        for (Attendance att1 : attendanceList) {
            if (flag.equals("true")){
                if (att1.getOffWorkAmMsg().equals("合格") &&
                        att1.getOffWorkPmMsg().equals("合格")&&
                        att1.getOnWorkAmMsg().equals("合格")&&
                        att1.getOnWorkPmMsg().equals("合格")
                ){
                    attendances.add(att1);
                }
            }
        }




//        ArrayList<Attendance> newAttendanceList = new ArrayList<>();
//
//        if (attendance.getIsQualified() != null) {
//            String[] isQualified = attendance.getIsQualified();
//            System.out.println("打印级联数组的数据：" + Arrays.toString(isQualified));
//            //采用迭代器
//            Iterator<Attendance> iterator = attendanceList.iterator();
//            //级联框选择数据
////            for (Attendance att : attendanceList) {
//            while (iterator.hasNext()) {
//                //判断选择的时间
//                String is_day = isQualified[0];
//                String is_standard = isQualified[1];
//                System.out.println("时间数据：" + is_day);
//                System.out.println("是否合格------" + is_standard);
//                //System.out.println("判断时间"+is_day);
//                //判断：上午上班时间+合格
//                if ("上午上班时间".equals(is_day) && "合格".equals(iterator.next().getOnWorkAmMsg())) {
////                    if (iterator.next().getOffWorkAmMsg() != null) {
//                    //不为空添加
////                    newAttendanceList.add(iterator.next());
////                    attendance.setzaotui(8:00)
//                    attendance.setAmGoZao("早退了");
//                    attendanceMapper.selectAttendanceList(attendance);
//                }
//            }
//        }

//        attendance.getIsQualified();// 上午上班时间/合格  [上午上班时间, 合格]


        if (attendances.size()==0){
            return attendanceList;
        }else {
            return attendances;
        }
        //return attendanceList == null ? newAttendanceList : attendanceList;
    }

    /**
     * 新增出勤管理
     *
     * @param attendance 出勤管理
     * @return 结果
     */
    @Override
    public boolean insertAttendance(Attendance attendance) {
//        Date onWorkAm = attendance.getOnWorkAm();
//        long onWorkAm_ms = onWorkAm.getTime();
//        long now_day_onWorkAm = onWorkAm_ms / 3600 / 24 / 1000;
//
//        Date offWorkAm = attendance.getOffWorkAm();
//        long offWorkAm_ms = offWorkAm.getTime();
//        long now_day_offWorkAm = offWorkAm_ms / 3600 / 24 / 1000;
//
//        Date onWorkPm = attendance.getOnWorkPm();
//        long onWorkPm_ms = onWorkPm.getTime();
//        long now_day_onWorkPm = onWorkPm_ms / 3600 / 24 / 1000;
//
//        Date offWorkPm = attendance.getOffWorkPm();
//        long offWorkPm_ms = offWorkPm.getTime();
//        long now_day_offWorkPm = offWorkPm_ms / 3600 / 24 / 1000;
//
//        Date recordTime = attendance.getRecordTime();
//        long recordTime_ms = recordTime.getTime();
//        long now_day_recordTime = recordTime_ms / 3600 / 24 / 1000 ;00Z
//
//        if (now_day_onWorkAm == now_day_offWorkAm && now_day_onWorkPm == now_day_offWorkPm && now_day_offWorkPm == now_day_recordTime) {
//            attendanceMapper.insertAttendance(attendance);
//            return true;
//        } else {
//            return false;
//        }

        Date onWorkAm = attendance.getOnWorkAm();
        Date offWorkAm = attendance.getOffWorkAm();
        Date onWorkPm = attendance.getOnWorkPm();
        Date offWorkPm = attendance.getOffWorkPm();
        Date recordTime = attendance.getRecordTime();
        boolean isAmSameDay = DateUtils.isSameDay(onWorkAm, offWorkAm);
        boolean isPmSameDay = DateUtils.isSameDay(onWorkPm, offWorkPm);
        boolean isSameDay = DateUtils.isSameDay(offWorkPm, recordTime);
        if (isAmSameDay && isPmSameDay && isSameDay) {
            attendanceMapper.insertAttendance(attendance);
            return true;
        } else {
            return false;
        }

        //return attendanceMapper.insertAttendance(attendance);
    }

    /**
     * 修改出勤管理
     *
     * @param attendance 出勤管理
     * @return 结果
     */
    @Override
    public boolean updateAttendance(Attendance attendance) {
//        Date onWorkAm = attendance.getOnWorkAm();
//        long onWorkAm_ms = onWorkAm.getTime();
//        long now_day_onWorkAm = onWorkAm_ms / 3600 / 24 / 1000;
//        System.out.println("修改上午上班时间：" + now_day_onWorkAm);
//        System.out.println("修改上午上班时间毫秒值：" + onWorkAm_ms);
//
//        Date offWorkAm = attendance.getOffWorkAm();
//        long offWorkAm_ms = offWorkAm.getTime();
//        long now_day_offWorkAm = offWorkAm_ms / 3600 / 24 / 1000;
//        System.out.println("修改上午下班时间：" + now_day_offWorkAm);
//        System.out.println("修改上午下班时间毫秒值：" + offWorkAm_ms);
//
//        Date onWorkPm = attendance.getOnWorkPm();
//        long onWorkPm_ms = onWorkPm.getTime();
//        long now_day_onWorkPm = onWorkPm_ms / 3600 / 24 / 1000;
//        System.out.println("修改下午上班时间：" + now_day_onWorkPm);
//        System.out.println("修改下午上班时间毫秒值：" + onWorkPm_ms);
//
//        Date offWorkPm = attendance.getOffWorkPm();
//        long offWorkPm_ms = offWorkPm.getTime();
//        long now_day_offWorkPm = offWorkPm_ms / 3600 / 24 / 1000;
//        System.out.println("修改下午下班时间：" + now_day_offWorkPm);
//        System.out.println("修改下午下班时间毫秒值：" + offWorkPm_ms);
//
//        Date recordTime = attendance.getRecordTime();
//        long recordTime_ms = recordTime.getTime();
//        long now_day_recordTime = recordTime_ms / 3600 / 24 / 1000;
//        System.out.println("修改今天时间：" + now_day_recordTime);
//        System.out.println("修改今天时间毫秒值：" + recordTime_ms);
//
//        if (now_day_onWorkAm == now_day_offWorkAm && now_day_onWorkPm == now_day_offWorkPm && now_day_offWorkPm == now_day_recordTime) {
//            attendanceMapper.updateAttendance(attendance);
//            return true;
//        } else {
//            return false;
//            //return true;
//        }

//        Date onWorkAm = attendance.getOnWorkAm();
//        Date offWorkAm = attendance.getOffWorkAm();
//        Date onWorkPm = attendance.getOnWorkPm();
//        Date offWorkPm = attendance.getOffWorkPm();
//        Date recordTime = attendance.getRecordTime();
//
//        int am_i = DateUtils.differentDaysByMillisecond(onWorkAm, offWorkAm);
//        int pm_i = DateUtils.differentDaysByMillisecond(onWorkPm, offWorkPm);
//        int date_i = DateUtils.differentDaysByMillisecond(offWorkPm, recordTime);
//
//        if (am_i == 0 && pm_i == 0 && date_i == 0) {
//            attendanceMapper.updateAttendance(attendance);
//            return true;
//        } else {
//            return false;
//        }


        Date onWorkAm = attendance.getOnWorkAm();
        Date offWorkAm = attendance.getOffWorkAm();
        Date onWorkPm = attendance.getOnWorkPm();
        Date offWorkPm = attendance.getOffWorkPm();
        Date recordTime = attendance.getRecordTime();
        boolean isAmSameDay = DateUtils.isSameDay(onWorkAm, offWorkAm);
        boolean isPmSameDay = DateUtils.isSameDay(onWorkPm, offWorkPm);
        boolean isSameDay = DateUtils.isSameDay(offWorkPm, recordTime);
        if (isAmSameDay && isPmSameDay && isSameDay) {
            attendanceMapper.updateAttendance(attendance);
            return true;
        } else {
            return false;
        }

        //return attendanceMapper.updateAttendance(attendance);
    }

    /**
     * 批量删除出勤管理
     *
     * @param atIds 需要删除的出勤管理主键
     * @return 结果
     */
    @Override
    public int deleteAttendanceByAtIds(Integer[] atIds) {
        return attendanceMapper.deleteAttendanceByAtIds(atIds);
    }

    /**
     * 删除出勤管理信息
     *
     * @param atId 出勤管理主键
     * @return 结果
     */
    @Override
    public int deleteAttendanceByAtId(Integer atId) {
        return attendanceMapper.deleteAttendanceByAtId(atId);
    }
}
